
import { SDKState } from "../SDKDefine";
import SDKAd from "./SDKAd";
export default class SDKAppBoxAd extends SDKAd {


    open() {
        console.log('QQAppBoxAd open ')
        // this.destroy()

        if (this._state == SDKState.SUCCESS) {
            this.show()
        } else {
            this.load()
        }


    }

    onClose() {
        console.log(' AppBox onClose ')
        this.setState(SDKState.CLOSE)
    }

    load() {
        if (!this.instance) {
            this.instance = this.sdk.createAppBox({ adUnitId: this._adUnitId })
            if (this.instance) {
                this.instance.onClose(this.bind(this.onClose))
                console.log("QQAppBoxAd  load 1111")
                if (this.instance) {
                    console.log("QQAppBoxAd  load ")
                    this.instance.load().then(() => {
                        console.log("QQAppBoxAd  load 2222 ")
                        this.setState(SDKState.SUCCESS)
                        // this.show()
                    }).catch((err: any) => {
                        console.log("QQAppBoxAd  load err", err)
                    })
                } else {
                    console.log(' instance is null ')
                }
            }

        }
    }

    show() {
        console.log("QQAppBoxAd  show 1111")
        if (this.instance) {

            this.instance.show().then(() => {
                console.log("QQAppBoxAd  show ")
                // this.load()
            }).catch((err: any) => {
                console.log("QQAppBoxAd  show ", err)

            })
        }
    }

    destroy() {
        if (this.instance) {
            this.instance.offClose(this.unbind(this.onClose))
            this.instance.destroy()
            this.instance = null;
        }
    }
}
